package infra

import (
	"encoding/json"
	"net/url"
	"time"
)

const (
	PayExpireDuration = time.Second * 60 * 15
	MaxTimestampSpan  = 60 * 5 // 最大时间戳容忍间隔，单位秒
)

type Attach struct {
	OrderType string `json:"order_type"`
}

func (a Attach) Json() string {
	b, _ := json.Marshal(a)
	return string(b)
}

func (a Attach) URLEncode() string {
	return url.QueryEscape(a.Json())
}

func NewAttachFromJson(text string) *Attach {
	var attach Attach
	if err := json.Unmarshal([]byte(text), &attach); err != nil {
		return nil
	}
	return &attach
}

func NewAttachFromURLEncodedJson(text string) *Attach {
	jsonText, err := url.QueryUnescape(text)
	if err != nil {
		return nil
	}
	return NewAttachFromJson(jsonText)
}
